package cn.com.open.itembank.controller;

import cn.com.open.itembank.common.GlobalConst;
import cn.com.open.itembank.dto.ReturnModel;
import cn.com.open.itembank.service.CourseRemoteService;
import cn.com.open.itembank.service.ItemBankRemoteService;
import cn.com.open.itembank.utils.JsonUtil;
import cn.com.open.itembank.utils.ListUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by lucky on 2017/8/28.
 */
@RestController
public class BaseController {
    private static final Logger logger = LoggerFactory.getLogger(BaseController.class);
    public static String appKeyString = "appkey";
    public static String appSecretString = "appsecret";

    @Autowired
    private CourseRemoteService courseService;
    @Autowired
    private ItemBankRemoteService itemBankService;

    /**
     * Response list
     *
     * @param response the response
     * @param data     the data
     */
    public <T> void responseList(HttpServletRequest request, HttpServletResponse response, List<T> data) {
        if (ListUtil.isNullOrEmpty(data)) {
            responseJson(request, response,data, "未查询到数据", GlobalConst.RESOURCE_NOT_EXIST);
        } else {
            responseJson(request, response,data);
        }
    }

    /**
     * Response json
     *
     * @param response the response
     * @param data     the data
     */
    public void responseJson(HttpServletRequest request, HttpServletResponse response, Object data) {
        responseJson(request, response,data, "");
    }

    /**
     * Response json
     *
     * @param response the response
     * @param data     the data
     * @param message  the message
     */
    public void responseJson(HttpServletRequest request, HttpServletResponse response, Object data, String message) {
        responseJson(request, response,data, message, GlobalConst.SUCCESS);
    }

    /**
     * Response json
     *
     * @param response the response
     * @param data     the data
     * @param message  the message
     * @param code     the code
     */
    public void responseJson(HttpServletRequest request, HttpServletResponse response, Object data, String message, int code) {
        ReturnModel result = new ReturnModel();
        result.setCode(code);
        result.setMessage(message);
        if (data != null) {
            result.setData(data);
        }

        try {
            String resultJson = JsonUtil.getJson(result);

            response.setContentType("application/json;charset=UTF-8");
            response.setHeader("Access-Control-Allow-Origin", "*");

            response.getWriter().write(resultJson);
            logger.info(resultJson);
        } catch (IOException ex) {
            logger.error(ex.getMessage());
        }
    }

    /**
     * 判断当前请求是否有操作指定课程的权限
     *
     * @param request the request
     * @return the boolean
     */
    public boolean isPermissionForCourse(HttpServletRequest request, String courseId) {
        String appKey = request.getHeader(appKeyString);

        // 确认除非非限定appid，否则只能修改appid对应的itemBankId下的数据
        return courseService.coursePermission(courseId, appKey);
    }

    /**
     * 判断当前请求是否有操作指定题库的权限
     *
     * @param request the request
     * @return the boolean
     */
    public boolean isPermissionForItemBank(HttpServletRequest request, String itembankId) {
        String appKey = request.getHeader(appKeyString);

        // 确认除非非限定appid，否则只能修改appid对应的itemBankId下的数据
        return itemBankService.hasPermission(itembankId, appKey);
    }

    /**
     * 判断给定appkey是否包含在配置文件的applist许可列表内，如果包含则说明拥有删除题库权的权利.
     *
     * @param request the request
     * @return the boolean
     */
    public boolean isPermissionForAppId(HttpServletRequest request) {
        String appkey = request.getHeader(appKeyString);
        return itemBankService.hasSuperPermission(appkey);
    }
}
